#include "Logger/tri_logger.hpp"
#include "TextEditor.h"
#include "Splitter.h"
#include <QWidget>
#include <QTextCharFormat>
#include <QFont>
#include <QAction>
#include <QTextCursor>



TextEditor::TextEditor(Splitter* parent) : QTextEdit(parent){
	TRI_LOG_STR("Gui TextEditor");	
	splitter = parent;
	connect(this, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)),
	             this, SLOT(currentCharFormatChanged(const QTextCharFormat &)));
}

void TextEditor::setBold(bool bold){
    QTextCharFormat fmt;
    fmt.setFontWeight(bold ? QFont::Bold : QFont::Normal);
    mergeFormatOnWordOrSelection(fmt);
}

void TextEditor::setItalic(bool italic){
	QTextCharFormat fmt;
	fmt.setFontItalic(italic);
	mergeFormatOnWordOrSelection(fmt);
}

void TextEditor::setUnderline(bool underline){
	QTextCharFormat fmt;
	fmt.setFontUnderline(underline);
	mergeFormatOnWordOrSelection(fmt);
}

void TextEditor::setTextFamily(const QString& familyIdentifier){
	TRI_LOG(familyIdentifier.toAscii().constData ()  );
    QTextCharFormat fmt;
    fmt.setFontFamily(familyIdentifier);
    mergeFormatOnWordOrSelection(fmt);
}

void TextEditor::setTextSize(const QString& sizeIdentifier){
	TRI_LOG_STR("setTextSize ");
    QTextCharFormat fmt;
	qreal pointSize = sizeIdentifier.toFloat();
	     if (pointSize > 0) {
	         QTextCharFormat fmt;
	         fmt.setFontPointSize(pointSize);
	         mergeFormatOnWordOrSelection(fmt);
	     }	
}



void TextEditor::focusInEvent ( QFocusEvent * event ) {
	TRI_LOG_STR("focusInEvent");
	splitter->currentCharFormatChanged(currentCharFormat ());
}


void TextEditor::currentCharFormatChanged(const QTextCharFormat &format){
	TRI_LOG_STR("currentCharFormatChanged");
	splitter->currentCharFormatChanged(format);
	
}

void TextEditor::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
 {
//     QTextCursor cursor = this->textCursor();
//     if (!cursor.hasSelection())
//         cursor.select(QTextCursor::WordUnderCursor);
//     if (cursor.hasSelection())
//    	 cursor.mergeCharFormat(format);
     mergeCurrentCharFormat(format);
     setFocus();

 }


